package com.framework.modules.sys.service.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.YHBDao.sys.YhbUserRoleClassDao;
import com.framework.modules.sys.service.YhbUserRoleClassService;
import com.framework.utils.MapUtils;
import com.framework.yhbEntity.sys.YhbUserRoleClass;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("yhbUserRoleClassService")
public class YhbUserRoleClassServiceImpl extends ServiceImpl<YhbUserRoleClassDao,YhbUserRoleClass> implements YhbUserRoleClassService {
    @Override
    public void saveOrUpdate(Long userId, List<Long> roleIdList) {
        // 先删除用户与角色关系
        this.deleteByMap(new MapUtils().put("yhb_sys_user_id", userId));

        if (roleIdList == null || roleIdList.size() == 0) {
            return;
        }

        // 保存用户与角色关系
        List<YhbUserRoleClass> list = new ArrayList<>(roleIdList.size());
        for (Long roleId : roleIdList) {
            YhbUserRoleClass sysUserRoleEntity = new YhbUserRoleClass();
            sysUserRoleEntity.setYhbSysUserId(userId);
            sysUserRoleEntity.setYhbRoleId(roleId);

            list.add(sysUserRoleEntity);
        }
        this.insertBatch(list);
    }

    @Override
    public List<Long> queryRoleIdList(Long userId) {
        return baseMapper.queryRoleIdList(userId);
    }

    @Override
    public int deleteBatch(Long[] roleIds) {
        return baseMapper.deleteBatch(roleIds);
    }
}
